package com.plwf.lake.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.plwf.lake.entity.Coach;
import com.plwf.lake.service.CoachService;
import com.plwf.lake.util.Uploader;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教练信息接口
 * @author 飘落晚风
 * @time:2019-5-12
 */
@RestController
@RequestMapping("/coach")
public class CoachCtrl {
    @Resource
    private CoachService service;

    //设置图片的物理存储路径
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**/**").addResourceLocations("file:F:/gradimages/lake/");

    }
    //查询所有教练 包含分页 条件查询(教练id)
    @RequestMapping("/query")
    public Map<String,Object> query(HttpServletRequest request){
        //查询条件
        String coaId = request.getParameter("coaId");
        Map<String,Object> map = new HashMap<>();
        if(coaId!=null && !coaId.equals("")){
            map.put("coaId",Integer.parseInt(coaId));
        }else{
            map.put("coaId",null);
        }
        //设置分页条件
        int page=1;
        int rows=5;
        String p = request.getParameter("page");
        if(p!=null && !p.equals("")){
            page = Integer.parseInt(p);
        }
        String r = request.getParameter("rows");
        if(r!=null && !r.equals("")){
            rows = Integer.parseInt(r);
        }
        //分页插件的工具类 ，传入当前页数和条数，并且先调用在查询
        PageHelper.startPage(page,rows);
        //查询出来的教练信息
        List<Coach> list = service.query(map);
        //获取分页信息，并且一并把分页信息转换为json 字符串传送出去
        Page pg = (Page)list;
        int pages = pg.getPages();
        Map<String,Object> rs = new HashMap<>();
        rs.put("list", list);
        rs.put("pages", pages);
        rs.put("page", page);
        return rs;

    }
    //保存教练信息
    @RequestMapping("/save")
    public Coach save(Coach bean){
        if(service.save(bean)){
            return bean;
        }else{
            return null;
        }

    }
    //删除教练信息 状态码设为-1
    @RequestMapping("/remove/{coaId}")
    public boolean remove(@PathVariable("coaId") Integer coaId){
        return service.remove(coaId);
    }

    //按教练id查询某个教练
    @RequestMapping("/find/{coaId}")
    public Coach find(@PathVariable("coaId") Integer coaId){
        return service.find(coaId);
    }

    //查询所有教练，不包含分页
    @RequestMapping("/findAll")
    public List<Coach> findAll(){
        return service.queryAll();
    }


    //修改状态码
    @RequestMapping("enable/{coaId}/{status}")
    public boolean enable(@PathVariable("coaId") Integer coaId,@PathVariable("status") Integer status){
        return service.enable(coaId,status);

    }
    //教练照片上传
    @RequestMapping("/upload")
    public boolean upload(MultipartFile picture,HttpServletRequest request){

        //获取图片存储的绝对系统路径aa
        String path = "F:/gradimages/lake";
        String coaId = request.getParameter("coaId");
        //
        String ctrl = "upload-";
        Integer id = Integer.parseInt(coaId);
        Coach coach =  new Coach();
        if(picture!=null && picture.getSize()>0){
            String pa = Uploader.upload(picture,path,ctrl,id);
            if(pa!=null){
                coach.setPicture(pa);
                coach.setCoaId(Integer.parseInt(coaId));
            }
        }
        return service.updateImage(coach);

    }
    //教练登陆信息
    @RequestMapping(value ="/login",method= RequestMethod.POST)
    public Coach login(String username, String password){
        Coach bean = new Coach();
        bean.setNumber(username);
        bean.setPassword(password);
        bean = service.login(bean);
        return bean;
    }
    //获得教练最大人数
    @RequestMapping("/maxId")
    public Integer querymaxId(){
        return service.maxId();
    }

}
